package com.prothink.threadadmindemo.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;

import javax.crypto.SecretKey;

/**
 * @author mayanping
 * @title: JWTToken
 * @projectName ThreadAdminDemo
 * @description: TODO
 * @date 2024/9/1608:54
 */
public class JWTTokenUtil {
    public static SecretKey key = Jwts.SIG.HS256.key().build();

    /**
     * 生成JWTToken
     * @return
     */
    public static String getToken(String account){
        String jws = Jwts.builder().subject(account).signWith(key).compact();
        return jws;
    }

    /**
     * 解析JWTToken
     * @param token
     * @return
     */
    public static String parseToken(String token) throws Exception{
        Jws<Claims> jws = Jwts.parser().verifyWith(key).build().parseSignedClaims(token);
        return jws.getPayload().getSubject();
    }
}
